package first.endtoend;


import first.endtoend.helpers.DialogHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;




public class ConfigurationActivity extends Activity{
	
	EditText url_edt, port_nb_edt, module_edt;
	Button done_btn;
	AlertDialog.Builder alert;
	ProgressDialog dialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_configuration);
		getActionBar().hide();
	   
		url_edt= (EditText) findViewById(R.id.edt_item_url);
		port_nb_edt= (EditText) findViewById(R.id.edt_item_port_nb);
		module_edt= (EditText) findViewById(R.id.edt_module);
		done_btn= (Button) findViewById(R.id.done_btn);
		
		
		done_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			
				/**
				 * Regex to Check the URL entered by the user 
				 * */
				String valid_url ="(@)?(href=')?(HREF=')?(HREF=\")?(href=\")?(http://)?[a-zA-Z_0-9\\-]+(\\.\\w[a-zA-Z_0-9\\-]+)+(/[#&\\n\\-=?\\+\\%/\\.\\w]+)?";
				String server_path="";
				try
				{
					server_path = url_edt.getText().toString();
					System.out.println("### the user entered a URL: "+server_path);
				}catch(Exception e)
				{
					System.out.println(e.getMessage());
				}
				if(server_path.length()>0){
					if(server_path.matches(valid_url)){
						System.out.println("url entered by the user is valid");
					}
					else{
						System.out.println("url entered by the user is not valid !!!!");
						alert = new AlertDialog.Builder(ConfigurationActivity.this);
						DialogHelper.showErrorDialog(alert, getString(R.string.error),getString(R.string.url_error_msg));
					
						
						}		
					
				}
				else {
					System.out.println("user didn't enter a URL !!!");
					alert = new AlertDialog.Builder(ConfigurationActivity.this);
					DialogHelper.showErrorDialog(alert, getString(R.string.error),getString(R.string.empty_url_msg));
					
					
				}
				
				/**
				 * Regex to check the port number entered by the user 
				 * */
				String valid_server_port ="^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$";
				String server_port= "";
				try
				{
					server_port= port_nb_edt.getText().toString();
					System.out.println("### the user entered a port nb: "+server_port);
				}catch(Exception e)
				{
					System.out.println(e.getMessage());
				}
				if(server_port.length()>0){
					if(server_port.matches(valid_server_port)){
						System.out.println("port number entered by the user is valid");
						//Integer server_port_number= Integer.parseInt(server_port);
					}
					else{
						System.out.println("port number entered by the user is not valid !!!");
						alert = new AlertDialog.Builder(ConfigurationActivity.this);
						DialogHelper.showErrorDialog(alert, getString(R.string.error),
								getString(R.string.port_error_msg));
						}	
				}
				else {
					System.out.println("user didn't enter a port number!!!");
					alert = new AlertDialog.Builder(ConfigurationActivity.this);
					DialogHelper.showErrorDialog(alert, getString(R.string.error),
							getString(R.string.empty_port_msg));
				}
				
				
				String server_module= module_edt.getText().toString();
				
				if(server_module.length()>0){
					System.out.println("### module name entered by the user is :"+server_module);
				}
				else {
					System.out.println("user didn't enter a module name.");
					alert = new AlertDialog.Builder(ConfigurationActivity.this);
					DialogHelper.showErrorDialog(alert, getString(R.string.error),
							getString(R.string.empty_module_msg));
				}
				
				SharedPreferences.Editor settings = getSharedPreferences("my_pref_settings", 0).edit();
				settings.putString("url", server_path);
				settings.putString("portNumb", server_port);
				settings.putString("module", server_module);
				
				settings.commit();

				
				Intent intent = new Intent (ConfigurationActivity.this, LoginActivity.class);
//				intent.putExtra("url", server_path);
//				intent.putExtra("portNumb", server_port_number);
//				intent.putExtra("module", server_module);
				startActivity(intent);
				finish();
				
			}
		});	
		
		
	}
	/**
	 * method to show alert while user enter invalid data 
	 * */
	public void showErrorDialog(String message, final int action) {
		AlertDialog.Builder alert = new AlertDialog.Builder(ConfigurationActivity.this);
		alert.setIcon(android.R.drawable.ic_delete);
		alert.setTitle(R.string.error);
		alert.setMessage(message);
		alert.setPositiveButton(R.string.retry,	new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		alert.setNegativeButton(R.string.close,	new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		alert.show();
	}
	
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if(dialog != null){
			dialog.dismiss();
			dialog= null;
		}
	}
	

}
